//
//  MyAPI.swift
//  swiftProductTemplate
//
//  Created by 刘圣洁 on 2025/6/20.
//

import Moya
import RxSwift

private let base_url = "https://api.example.com"

///示例，开发时删除，按自己的项目写
enum MyAPI {
    case fetchUsers
    case createUser(String, Int)
    case fetchUser(Int)
}

extension MyAPI: TargetType {
    var baseURL: URL {
        return URL(string: base_url)!
    }
    
    var path: String {
        switch self {
        case .fetchUsers:
            return "/users"
        case .createUser:
            return "/users"
        case .fetchUser(let id):
            return "/users/\(id)"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .fetchUsers, .fetchUser:
            return .get
        case .createUser:
            return .post
        }
    }
    
    var task: Task {
        switch self {
        case .fetchUsers, .fetchUser:
            return .requestPlain
        case .createUser(let name, let age):
            return .requestParameters(
                parameters: ["name": name, "age": age],
                encoding: JSONEncoding.default
            )
        }
    }
    
    var headers: [String: String]? {
        return ["Content-Type": "application/json"]
    }
}
